<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="../../layui/css/layui.css" type="text/css">
    <style>
        body{
            padding: 20px;
        }
    </style>
</head>
<body>
        <form class="layui-form" >
            <div class="layui-form-item">
                <label class="layui-form-label">商品名称</label>
                <div class="layui-input-block">
                <input type="text" name="gname" required  lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">价格</label>
                <div class="layui-input-block">
                <input type="text" name="price" required  lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">上架</label>
                <div class="layui-input-block">
                <input type="checkbox" name="status" lay-skin="switch" lay-text="Y|N" lay-filter="status">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">描述</label>
                <div class="layui-input-block">
                <input type="text" name="description" required  lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">库存</label>
                <div class="layui-input-block">
                <input type="text" name="stock" required  lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">商品编号</label>
                <div class="layui-input-block">
                <input type="text" name="identifier" required  lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
           
            <div class="layui-form-item">
                <label class="layui-form-label">回收</label>
                <div class="layui-input-block">
                <input type="checkbox" name="recycle" lay-skin="switch" lay-text="Y|N" lay-filter="recycle">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">商品分类</label>
                <div class="layui-input-block">
                <input type="text" name="cid" required  lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">热销</label>
                <div class="layui-input-block">
                <input type="checkbox" name="is_best" lay-skin="switch" lay-text="Y|N" lay-filter="is_best">
                </div>
            </div>
         
           
            <div class="layui-form-item">
                <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>


    <script src="../../layui/layui.js"></script>
    <script src="../../js/ajax.js"></script>
    <script>

        function child(obj, fn){
            //console.log(obj)
            var is_best = obj.is_best,
                recycle = obj.recycle,
                status = obj.status;
            
            var index = parent.layer.getFrameIndex(window.name);
            layui.use('form', function(){
                var form = layui.form;
                //form.render();
                // 监听开关事件
                form.on('switch(is_best)', function(obj){
                    if(this.checked){
                        this.value = 'yes';
                    }else{
                        this.value = 'no';
                    }
                    //console.log(this.value)
                    is_best = this.value;
                        //console.log(obj)
                        //console.log(this)
                        //layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
                });
                form.on('switch(recycle)', function(obj){
                    if(this.checked){
                        this.value = 'yes';
                    }else{
                        this.value = 'no';
                    }
                    recycle = this.value;
                });
                form.on('switch(status)', function(obj){
                    if(this.checked){
                        this.value = 'yes';
                    }else{
                        this.value = 'no';
                    }
                    status = this.value;
                });



                //监听提交
                form.on('submit(formDemo)', function(data){
                    // 开启加载动画
                    var index2 = layer.load(2, {time: 10*1000});
                    //layer.msg(JSON.stringify(data.field));

                    //data.field.fieldstatus = 
                    
                    //console.log(is_best, recycle, status);
                    data.field.status = status;
                    data.field.recycle = recycle;
                    data.field.is_best = is_best;
                    //console.log(data.field);
                    ajax({
                        url: '../php/updata-goods.php',
                        data: {
                            data: JSON.stringify(data.field),
                            id_name: 'gid',
                            id: obj.gid
                        },
                        method: 'post',
                        success: function(res){
                            // 关闭加载动画
                            layer.close(index2); 
                            fn(data.field);
                            if(res == 1){
                                layer.msg('更新成功'); 
                                //return data.field;
                                //parent.location.reload();
                                  
                            }else{
                                layer.msg('更新失败'); 
                                //return false;
                            }
                              
                            parent.layer.close(index); //再执行关闭   
                        }
                    });
                    return false;
                });


                


            })
        }
    </script>
</body>
</html>